/**
 * boxTypes每个元素表示[箱子数,每个箱子装载量]
 * boxTypes = [[5, 10], [2, 5], [4, 7], [3, 9]], truckSize = 10
* @param {number[][]} boxTypes
* @param {number} truckSize
* @return {number}
*/
var maximumUnits = function (boxTypes, truckSize) {
    // 按装载量排序
    boxTypes.sort((a, b) => {
        return b[1] - a[1]
    })

    let count = 0
    for (let i = 0; i < boxTypes.length; i++) {
        if (truckSize === 0) break

        if (truckSize >= boxTypes[i][0]) {
            // 箱子数 * 每个箱子装载量
            count += boxTypes[i][0] * boxTypes[i][1]
            truckSize -= boxTypes[i][0]
        } else {
            count += truckSize * boxTypes[i][1]
            truckSize = 0
        }
    }
    
    return count
};